﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Meqs.Entity;
using Meqs.Logic;
using Meqs.Logic.Interface;
using System.Web.UI.MobileControls;

public partial class Custom_SchoolYearMonth : System.Web.UI.UserControl
{
    protected IList<TermInfo> terms = new List<TermInfo>();
    protected List<YearMonthList> yearmonthlist = new List<YearMonthList>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            yearmonthlist = new List<YearMonthList>();
            SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", si.SchoolMark);
            //ht.Add("SchoolYear", si.SchoolYear);
            //ht.Add("Term", si.SchoolTerm);
            terms = Container<ITermInfoLogic>.Instance.FindAll(ht);
            DateTime now = DateTime.Now;
            int index = 0;
            foreach (TermInfo t in terms)
            {
                DateTime stime = Convert.ToDateTime( t.StartDate );
                DateTime etime = Convert.ToDateTime(t.EndDate); 
                if (index==0 )
                {
                    etime = DateTime.Now;
                }
                else
                {
                    etime =  Convert.ToDateTime(terms[index - 1].StartDate);               
                }
                YearMonthList yml = new YearMonthList();
                yml.schoolyear = t.SchoolYear;
                yml.term = t.Term;
                yml.months = GetYearAndMonth(t.SchoolYear, t.Term, stime, etime);
                yearmonthlist.Add(yml);
                index++;
            }
        }
    }

    private List<YearAndMonth> GetYearAndMonth(int schoolyear,int schoolterm,DateTime stime,DateTime etime)
    {
        List<YearAndMonth> ym = new List<YearAndMonth>();
        if (stime < etime)
        {
            if (stime.Year < etime.Year)
            {
                for (int m = stime.Month; m < 13; m++)
                {
                    YearAndMonth y = new YearAndMonth();
                    y.year = stime.Year;
                    y.month = m;
                    ym.Insert(0,y);
                }

                for (int m = 1; m <= etime.Month; m++)
                {
                    YearAndMonth y = new YearAndMonth();
                    y.year = etime.Year;
                    y.month = m;
                    ym.Insert(0, y);
                }
            }
            else
            {
                for (int m = stime.Month; m <= etime.Month; m++)
                {
                    YearAndMonth y = new YearAndMonth();
                    y.year = stime.Year;
                    y.month = m;
                    ym.Add(y);
                }

            }
        }
        return ym;
    }
}
public class YearAndMonth
{
    public int year;
    public int month;
}
public class YearMonthList
{
    public int schoolyear;
    public int term;
    public List<YearAndMonth> months = new List<YearAndMonth>();
}